package edu.hebtu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Locale;

//扩展 springMvc          dispatcherServlet          springMvc的页面只能写在templates目录下
@Configuration
//@EnableWebMvc       //导入了一个类：DelegatingWebMvcConfiguration：从容器中获取所有的WebMvcConfigurer
public class MyMvcConfig implements WebMvcConfigurer {

    //ViewResolver  实现了试图解析器接口的类，可以把它看作视图解析器

    @Bean
    public ViewResolver myViewResolver() {
        return new MyViewResolver();
    }


    //自定义了一个自己的试图解析器MyViewResolver
    public static class MyViewResolver implements ViewResolver {

        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
        }
    }


    //视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/main.html").setViewName("main-pages/index-backstage");
        registry.addViewController("/login.html").setViewName("pages-signin");
        registry.addViewController("/recover.html").setViewName("pages-recover-password");
        registry.addViewController("/pages-user-profile.html").setViewName("pages/pages-user-profile");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/login.html", "/index.html", "/loginCL", "/index_login", "/recover.html", "/sign_recover",
                        "/ajax/**", "/css/**", "/fonts/**", "/images/**", "/javascripts/**", "/js/**", "/picture/**", "/stylesheets/**", "/vendor/**");
    }
}
